home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / rspf.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  58KB  |  1,898 lines

  1. /*             RSPF - Radio Shortest Path First
  2.  * This code may be used for non-commercial purposes only.
  3.  *            Anders Klemets SM0RGV
  4.  * 890918 - Version 2.0
  5.  * 900816 - Version 2.1
  6.  *
  7.  * Mods by PA0GRI
  8.  */
  9. #include "global.h"
  10. #include "config.h"
  11. #include "mbuf.h"
  12. #include "proc.h"
  13. #include "timer.h"
  14. #include "netuser.h"
  15. #include "internet.h"
  16. #include "pktdrvr.h"
  17. #include "ip.h"
  18. #include "iface.h"
  19. #include "ax25.h"
  20. #include "arp.h"
  21. #include "icmp.h"
  22. #include "socket.h"
  23. #include "rspf.h"
  24.  
  25. extern struct route *rt_lookup __ARGS((int32 target));
  26.  
  27. #ifdef    AX25
  28. extern struct lq *al_lookup __ARGS((struct iface *ifp,char *addr,int sort));
  29. #else
  30. static struct lq *al_lookup __ARGS((struct iface *ifp,char *addr,int sort));
  31. #endif    /* AX25 */
  32.  
  33. struct rspf_stat Rspf_stat;
  34. struct rspfreasm *Rspfreasmq = NULLRREASM;
  35. struct rspfiface *Rspfifaces = NULLRIFACE;
  36. struct rspfadj *Adjs = NULLADJ;
  37. struct rspfrouter *Rspfrouters = NULLRROUTER;
  38. struct mbuf *Rspfinq = NULLBUF;
  39. struct timer Rspfreasmt, Susptimer;
  40. char *Rrh_message = NULLCHAR;
  41. unsigned short Rspfpingmax = 3;
  42. static int Keeprouter = 0;
  43. static int16 Envelopeid = 0;
  44. static int Nrspfproc = 0;
  45. static unsigned char Rspfsubseq = 0;
  46. static short Rspfseq = -32768 + 1;
  47. static char *findlowroute __ARGS((int32 addr,int *low,int add,int32 *resaddr,int *resbits));
  48. static void makeroutes __ARGS((void));
  49. static void partialupdate __ARGS((struct rspfrouter *rr,struct mbuf *bp));
  50. static struct rspfrouter *rr_lookup __ARGS((int32 addr));
  51. static void rr_delete __ARGS((int32 addr));
  52. static struct rspfiface *rtif_lookup __ARGS((int32 addr));
  53. static void rspfcsum __ARGS((struct mbuf **bpp,int32 dest));
  54. static void update_in __ARGS((struct mbuf *bp,int32 addr));
  55. static void node_in __ARGS((struct mbuf *bp,int32 addr,int full));
  56. static void sendonerspf __ARGS((int32 addr,int32 dest));
  57. static void sendtoall __ARGS((struct mbuf *bp,int nodecnt,struct rspfiface *riface));
  58. static int sendupdate __ARGS((struct mbuf *bp,int nodecnt,int32 addr));
  59. static int isnewer __ARGS((short a,short b));
  60. static void del_reasm __ARGS((struct rspfreasm *re));
  61. static void reasmtimeout __ARGS((void *t));
  62. static struct mbuf *rspfreasm __ARGS((struct mbuf *bp,struct rspfpacketh *rph,int32 addr));
  63. static struct mbuf *fragmenter __ARGS((struct mbuf *bp,int nodes,int16 mtu));
  64. static struct mbuf *makeadjupdate __ARGS((int32 addr,struct rspfrouter *rr));
  65. static void pushadj __ARGS((struct mbuf **bpp,int32 addr,int bits));
  66. static void sendrrh __ARGS((struct rspfiface *ri));
  67. static void sendrspf __ARGS((void));
  68. static void goodbadnews __ARGS((struct rspfadj *adj));
  69. static void add_rspfadj __ARGS((struct rspfadj *adj));
  70. static void rspfcheck __ARGS((struct rspfadj *adj));
  71. static void rspfping __ARGS((int oldstate,void *p,void *v));
  72.  
  73. /* IP passes its RSPF packets to this function */
  74. void
  75. rspf_input(iface,ip,bp,rxbroadcast)
  76. struct iface *iface;
  77. struct ip *ip;
  78. struct mbuf *bp;
  79. int rxbroadcast;
  80. {
  81.     struct pseudo_header ph;    /* Pseudo-header for checksumming */
  82.  
  83.     if(bp == NULLBUF)
  84.         return;
  85.     if(Rspfifaces == NULLRIFACE){    /* Rspf main loop is not running */
  86.         free_p(bp);
  87.         return;
  88.     }
  89.     ph.length = ip->length - IPLEN - ip->optlen;
  90.     ph.source = ip->source;
  91.     ph.dest = ip->dest;
  92.     ph.protocol = ip->protocol;
  93.     if(cksum(&ph,bp,ph.length) != 0){
  94.         /* Checksum failed, ignore packet completely */
  95.         free_p(bp);
  96.         ++Rspf_stat.badcsum; 
  97.         return;
  98.     }
  99.     bp = pushdown(bp,1 + sizeof(ip->source) + sizeof(iface));
  100.     *bp->data = RSPFE_PACKET;
  101.     memcpy(bp->data + 1,&ip->source,sizeof(ip->source));
  102.     memcpy(bp->data + 1 + sizeof(ip->source),&iface,sizeof(iface));
  103.     enqueue(&Rspfinq,bp);
  104. }
  105.  
  106. /* Main loop in RSPF process */
  107. void
  108. rspfmain(v,v1,v2)
  109. int v;
  110. void *v1,*v2;
  111. {
  112.     union rspf rspf;        /* Internal packet structure */
  113.     struct rspfadj *adj = NULLADJ;    /* Adjacency */
  114.     struct mbuf *bp, *tbp;
  115.     struct rspfiface *riface;
  116.     struct iface *iface;
  117.     struct route *rp;
  118.     int32 source;            /* Source address */
  119.     int cmd;
  120.     
  121.     for(;;) {
  122.          if(Rspfinq == NULLBUF)
  123.           pwait(&Rspfinq);
  124.          bp = dequeue(&Rspfinq);    /* at least 5 bytes */
  125.          cmd = PULLCHAR(&bp);    /* get internal event indicator */
  126.          pullup(&bp,(char *)&source,sizeof(source));
  127.          switch(cmd) {
  128.          case RSPFE_RRH:
  129.           sendrrh((struct rspfiface *)source);
  130.           break;
  131.          case RSPFE_CHECK:
  132.           rspfcheck((struct rspfadj *)source);
  133.           break;
  134.          case RSPFE_UPDATE:
  135.           sendrspf();
  136.           break;
  137.          case RSPFE_ARP:
  138.           adj = (struct rspfadj *) source;
  139.           source = adj->addr;        /* fall through */
  140.          case RSPFE_PACKET:
  141.           pullup(&bp,(char *)&iface,sizeof(iface));
  142.           break;
  143.          }
  144.          if(cmd != RSPFE_PACKET && cmd != RSPFE_ARP)
  145.           continue;
  146.          if(cmd == RSPFE_PACKET && ntohrspf(&rspf,&bp) == -1){
  147.           free_p(bp);
  148.           continue;
  149.          }
  150.          if(iface != NULLIF) {
  151.           for(riface = Rspfifaces; riface != NULLRIFACE; riface =
  152.               riface->next)
  153.                if(riface->iface == iface)
  154.                break;
  155.          } else
  156.           /* fails if there is no route or no RSPF interface */
  157.           riface = rtif_lookup(source);
  158.  
  159.          if(riface == NULLRIFACE) {
  160.           free_p(bp);
  161.           if(cmd == RSPFE_PACKET)
  162.                ++Rspf_stat.norspfiface;
  163.           continue;    /* We do not use RSPF on this interface */
  164.          }
  165.          /* If we dont have an entry in the routing table for this station,
  166.           * or if the entry is less than 32 bits specific or has a higher
  167.           * cost than our new route, and is pointing to the wrong interface,
  168.           * then add a correct, private, route.
  169.           */
  170.          rp = rt_lookup(source);
  171.          if(rp == NULLROUTE || (rp != NULLROUTE && rp->iface !=
  172.             riface->iface && (rp->bits < 32 || rp->metric >
  173.                       riface->quality))){
  174.           rt_add(source,32,0,iface,riface->quality,0,1);
  175.          }
  176.  
  177.          if(cmd == RSPFE_ARP) {
  178.         if(adj != NULLADJ){
  179.           adj->cost = riface->quality;    /* default cost */
  180.           add_rspfadj(adj);
  181.           continue;
  182.         }
  183.          }
  184.          switch(rspf.hdr.type){        /* packet types */
  185.          case RSPF_RRH:
  186.           ++Rspf_stat.rrhin;
  187.           free_p(bp);
  188.           adj = (struct rspfadj *)callocw(1,sizeof(struct rspfadj));
  189.           adj->addr = rspf.rrh.addr;
  190.           adj->seq = rspf.rrh.seq;
  191.           adj->cost = riface->quality;    /* Default cost */
  192.           adj->state = RSPF_TENTATIVE;
  193.           if(rspf.rrh.flags & RSPFMODE)
  194.                adj->tos = DELAY;
  195.           else
  196.                adj->tos = RELIABILITY;
  197.           add_rspfadj(adj);
  198.           break;
  199.          case RSPF_FULLPKT:
  200.           ++Rspf_stat.updatein;
  201.           /* Feed the packet through the reassembly queue */
  202.           if((tbp = rspfreasm(bp,&rspf.pkthdr,source)) != NULLBUF)
  203.                update_in(tbp,source);
  204.           break;
  205.          }
  206.     }
  207. }
  208.  
  209. /* This function is called every time an RRH should be broadcasted.
  210.  * An RRH is sent on the interface given by ri or on every RSPF interface.
  211.  * The broadcast address of the interface must be routed onto the same
  212.  * interface (using a static route for instance) otherwise there will be no
  213.  * RRH sent on that interface.
  214.  */
  215. static void
  216. sendrrh(ri)
  217. struct rspfiface *ri;        /* interface to use, if specified */
  218. {
  219.     struct pseudo_header ph;
  220.     struct mbuf *bp, *data = NULLBUF;
  221.     struct rspfiface *riface;
  222.     struct route *rp;
  223.     struct rrh rrh;
  224.  
  225.     for(riface = Rspfifaces; riface != NULLRIFACE; riface = riface->next) {
  226.      if(ri != NULLRIFACE && riface != ri)
  227.           continue;
  228.      if((rp = rt_lookup(riface->iface->broadcast)) != NULLROUTE &&
  229.         rp->iface == riface->iface){
  230.           rrh.version = RSPF_VERSION;
  231.           rrh.addr = riface->iface->addr;
  232.           ph.length = 0;
  233.           if(Rrh_message != NULLCHAR) {
  234.            data = ambufw((int16)strlen(Rrh_message));
  235.            memcpy(data->data,Rrh_message,strlen(Rrh_message));
  236.            ph.length = data->cnt = strlen(Rrh_message);
  237.           }
  238.           ph.length += RRHLEN;
  239.           ph.source = riface->iface->addr;
  240.           ph.dest = riface->iface->broadcast;
  241.           ph.protocol = RSPF_PTCL;
  242.           /* Start the RRH link level packet counter at 1, since adj->seq
  243.            * is 0 only for ARP generated adjacencies. This is also correct
  244.            * since rawsndcnt will increase by one when the RRH is sent.
  245.            */
  246.           rrh.seq = riface->iface->rawsndcnt + 1;
  247.           /* Default is to use the same mode as the interface */
  248.           if(Rspfownmode == -1)
  249.            rrh.flags = !(riface->iface->flags & CONNECT_MODE);
  250.           else
  251.            rrh.flags = !(Rspfownmode & CONNECT_MODE);
  252.  
  253.           bp = htonrrh(&rrh,data,&ph);
  254.           ++Rspf_stat.rrhout;
  255.           ip_send(riface->iface->addr,riface->iface->broadcast,RSPF_PTCL,
  256.               0,2,bp,0,0,0); /* the ttl will be decremented to 1 */
  257.      }
  258.     }
  259. }
  260.  
  261. /* This function is called whenever an RRH packet has been received or when
  262.  * a new entry is added to the ARP table.
  263.  */
  264. static void
  265. add_rspfadj(adj)
  266. struct rspfadj *adj;
  267. {
  268.     struct rspfadj *oldadj, *tmp;
  269.     struct rspfiface *riface;
  270.     struct arp_tab *atp;
  271.     struct lq *alp;
  272.     int dif, origdif;
  273.  
  274.     if(adj == NULLADJ)
  275.     return;            /* sanity but it happens... */
  276.     /* Find the previous copy of the adjacency, if there was any */
  277.     /* Insert the new adjacency */
  278.     adj->next = Adjs;
  279.     Adjs = adj;
  280.     for(oldadj = Adjs; oldadj->next != NULLADJ; oldadj = oldadj->next)
  281.     if(oldadj->next->addr == adj->addr) {
  282.         tmp = oldadj->next;
  283.         oldadj->next = oldadj->next->next;
  284.         oldadj = tmp;
  285.         break;
  286.     }
  287.  
  288.     if(oldadj->addr != adj->addr || oldadj == adj)
  289.     oldadj = NULLADJ;
  290.  
  291.     /* ARP entries give no sequence number, so save the old one */
  292.     if(oldadj != NULLADJ) {
  293.      if(adj->seq == 0)
  294.           adj->seq = oldadj->seq;
  295.      if(adj->tos == 0)
  296.           adj->tos = oldadj->tos;    /* they give no TOS either */
  297.     }
  298.  
  299.     switch(adj->state) {
  300.     case RSPF_TENTATIVE:
  301.     if(oldadj != NULLADJ) {
  302.          /* If the adjacency was in OK state, it will remain there.
  303.           * An RRH or ARP upcall can never generate an OK -> Tentative
  304.           * transition.
  305.           */
  306.          if(oldadj->state == RSPF_OK)
  307.          adj->state = RSPF_OK;
  308.          adj->okcnt = oldadj->okcnt;
  309.          /* If old state was Bad, don't announce this adjacency until
  310.           * it is known to be OK, to prevent announcing an adjacency
  311.           * with an state transition loop such as OK -> Suspect -> Bad ->
  312.           * Tentative -> Suspect -> Bad -> Tentative -> ... 
  313.           */
  314.          if(oldadj->state == RSPF_BAD) {
  315.           adj->okcnt = 0;
  316.           stop_timer(&oldadj->timer);
  317.           /* Enter Suspect state at once, there is no point in
  318.            * dwelling as Tentative.
  319.            */
  320.           rspfcheck(adj);
  321.          } else {
  322.           /* Inherit the old timer */
  323.           adj->timer.func = rspfevent;
  324.           adj->timer.arg = (void *) &adj->timer;
  325.           /* continue where the old timer is stopped */
  326.           adj->timer.duration = oldadj->timer.duration;
  327.           stop_timer(&oldadj->timer);
  328.           /* Set the timer to Suspectimer in the unlikely event that
  329.            * the timer was stopped and oldadj is not Suspect or Bad.
  330.            * Suspect state is dealt with further below.
  331.            */
  332.           if(dur_timer(&adj->timer) == 0L)
  333.                set_timer(&adj->timer,dur_timer(&Susptimer));
  334.           start_timer(&adj->timer);
  335.           set_timer(&adj->timer,dur_timer(&oldadj->timer));
  336.         }
  337.         /* We must now try to figure out from which AX.25 callsign this
  338.          * packet came. Looking at the ARP table may sometimes help. 
  339.          */
  340.         if(oldadj->seq != 0 && adj->seq != 0 &&
  341.            (riface = rtif_lookup(adj->addr)) != NULLRIFACE &&
  342.            (atp = arp_lookup(ARP_AX25,adj->addr,riface->iface)) != NULLARP &&
  343.            atp->state == ARP_VALID &&
  344.            (alp = al_lookup(riface->iface,atp->hw_addr,0)) != NULLLQ){
  345.         /* The wrapping of the modulus is taken into account here */
  346.         if(oldadj->seq > (MAXINT16 - 100) && adj->seq < 100)
  347.             origdif = adj->seq + MAXINT16 - oldadj->seq;
  348.         else
  349.             origdif = adj->seq - oldadj->seq;
  350.         if((alp->currxcnt - adj->heard) > (MAXINT16 - 100)
  351.            && adj->seq < 100)
  352.             dif = origdif + MAXINT16 - (alp->currxcnt - adj->heard);
  353.         else
  354.             dif = origdif - (alp->currxcnt - adj->heard);
  355.         adj->heard = alp->currxcnt;    /* Update the counter */
  356.         /* At this point, "origdif" equals the difference in sequence
  357.          * numbers between the two latest RRH packets, i.e. the
  358.          * number of AX.25 frames the station has sent during since
  359.          * the last RRH packet we heard. "dif" equals the number of
  360.          * AX.25 frames that we actually heard from that station
  361.          * during the same period.
  362.          */
  363.         if(origdif > 0 && dif > 0)
  364.             /* This algorithm can be improved, see 2.1 spec */
  365.             adj->cost = riface->quality*2-riface->quality*dif/origdif;
  366.         }
  367.         /* Ignore any new RRH's if a pinger process is already running */
  368.         if(oldadj->state == RSPF_SUSPECT) {
  369.          if(adj->heard != 0)        /* save new heard count */
  370.               oldadj->heard = adj->heard;
  371.          oldadj->next = adj->next;    /* adj is at start of chain */
  372.          Adjs = oldadj;
  373.          stop_timer(&adj->timer);
  374.          free((char *)adj);
  375.          return;
  376.         }
  377.         } else {                    /* oldadj == NULLADJ */
  378.         rspfcheck(adj);            /* enter Suspect state */
  379.         /* A new adjacency may affect the routing table even though no
  380.          * routing updates have yet been received from it.
  381.          */
  382.         makeroutes();
  383.     }
  384.     break;
  385.     case RSPF_OK:
  386.     if(oldadj != NULLADJ) {
  387.         adj->okcnt += oldadj->okcnt;    /* Do these before possible */
  388.         stop_timer(&oldadj->timer);        /* killproc() -- KZ1F */
  389.         if(oldadj->state == RSPF_SUSPECT){
  390.         killproc(oldadj->pinger);
  391.         --Nrspfproc;            /* Bug fix - N1BEE */
  392.         }
  393.     }
  394.     else
  395.          makeroutes();            /* routing table may change */
  396.     if(adj->okcnt == 1)            /* A previously unkown route */
  397.         goodbadnews(adj);            /* ..that is good news */
  398.     break;
  399.     }
  400.     stop_timer(&oldadj->timer);        /* stop timer before free() -- KZ1F */
  401. #ifndef KZ1F
  402.     free((char *)oldadj);
  403. #endif
  404. }
  405.  
  406. /* Take appropriate action if an adjacency is Bad or Tentative */
  407. static void
  408. rspfcheck(adj)
  409. struct rspfadj *adj;
  410. {
  411.     struct rspfadj *prev;
  412.  
  413.     for(prev = Adjs; prev != NULLADJ; prev = prev->next)
  414.      if(prev->next == adj)
  415.           break;
  416.     switch(adj->state){
  417.     case RSPF_OK:
  418.      adj->state = RSPF_TENTATIVE;     /* note fall-thru */
  419.     case RSPF_TENTATIVE:
  420.      if (Nrspfproc < RSPF_PROCMAX) {
  421.           Nrspfproc++;
  422.           adj->pinger = newproc("RSPF ping",1024,rspfping,
  423.                     (int)adj->state,adj,NULL,0);
  424.           adj->state = RSPF_SUSPECT; /* The adjacency is now Suspect */
  425.      } else {        /* Try later */
  426.           set_timer(&adj->timer,dur_timer(&Susptimer));
  427.           start_timer(&adj->timer);
  428.      }
  429.      break;
  430.     case RSPF_BAD:
  431.          rr_delete(adj->addr);
  432.      adj->cost = 255;
  433.      if(adj->okcnt != 0)
  434.           goodbadnews(adj);        /* This is bad news... */
  435.      if(prev != NULLADJ)
  436.           prev->next = adj->next;    /* Unlink */
  437.      else
  438.           Adjs = adj->next;
  439.      stop_timer(&adj->timer);    /* just in case */
  440.      free((char *)adj);        /* delete the adjacency */
  441.      makeroutes();            /* update the routing table */
  442.      break;
  443.     }
  444. }
  445.  
  446. /* Send a ping to a suspect or tentative adjacency */
  447. static void
  448. rspfping(oldstate, p, v)
  449. int oldstate;
  450. void *p, *v;
  451. {
  452.     int s, fromlen, pcnt;
  453.     struct icmp icmp;
  454.     struct rspfadj *adj;
  455.     struct sockaddr_in from;
  456.     struct mbuf *bp;
  457.  
  458.     pause(((ptol(p) & 7)+1)*1000L);    /* Pause for 1 to 8 seconds */
  459.     adj = (struct rspfadj *) p;
  460.     adj->timer.func = rspfevent;        /* Fill in timer values */
  461.     adj->timer.arg = (void *) &adj->timer;
  462.     set_timer(&adj->timer,dur_timer(&Susptimer));
  463.     if((s = socket(AF_INET,SOCK_RAW,ICMP_PTCL)) == -1){
  464.     --Nrspfproc;
  465.     adj->state = oldstate;
  466.     start_timer(&adj->timer);
  467.     return;
  468.     }
  469.  
  470.     fromlen = sizeof(from);
  471.     for(pcnt=0; pcnt < Rspfpingmax; ++pcnt) {
  472.     pingem(s,adj->addr,0,(int16)s,0);
  473.     /* Now collect the reply */
  474.     alarm(60 * 1000L);/* Let each ping timeout after 60 seconds */
  475.     for(;;) {
  476.          if(recv_mbuf(s,&bp,0,(char *)&from,&fromlen) == -1){
  477.           if(errno == EALARM){ /* We timed out */
  478.                break;
  479.           }
  480.           alarm(0);
  481.           adj->state = oldstate;
  482.           close_s(s);
  483.           --Nrspfproc;
  484.           start_timer(&adj->timer);
  485.           return;
  486.          }
  487.          ntohicmp(&icmp,&bp);
  488.          free_p(bp);
  489.          if(icmp.type != ICMP_ECHO_REPLY || from.sin_addr.s_addr !=
  490.         adj->addr || icmp.args.echo.id != s)
  491.           /* Ignore other people's responses */
  492.           continue;
  493.          alarm(0);
  494.          if(++adj->okcnt == 1)
  495.           goodbadnews(adj);        /* Good news */
  496.          close_s(s);
  497.          --Nrspfproc;
  498.          start_timer(&adj->timer);
  499.          adj->state = RSPF_OK;        /* Finally change state */
  500.          return;
  501.     }
  502.     }
  503.     /* we give up, mark the adjacency as bad */
  504.     adj->state = RSPF_BAD;
  505.     close_s(s);
  506.     --Nrspfproc;
  507.     start_timer(&adj->timer);
  508. }
  509.  
  510. /* ARP upcalls come in two flavors: When an ARP Reply has been received, we
  511.  * know for certain that bidirectional communication is possible with the
  512.  * particular station. But when an ARP Request is received, or if an ARP
  513.  * entry is added manually, it has yet to be determined if the link is
  514.  * bidirectional so iface is NULLIF in those cases.
  515.  */
  516. void
  517. rspfarpupcall(addr,hardware,iface)
  518. int32 addr;            /* Address being added to ARP table */
  519. int16 hardware;            /* Hardware type */
  520. struct iface *iface;        /* Interface used, if known */
  521. {
  522.     struct rspfadj *adj;
  523.     struct mbuf *bp;
  524.     struct rspfiface *riface;
  525.  
  526.     if((riface = rtif_lookup(addr)) == NULLRIFACE)
  527.      return;        /* Not a route to an RSPF interface */
  528.     /* Proceed only if the ARP entry is for a hardware type that is relevant
  529.      * for the interface onto which IP datagrams are routed.
  530.      */
  531.     switch(hardware) {
  532.     case ARP_NETROM:
  533.      if(riface->iface->type != CL_NETROM)
  534.           return;
  535.      break;
  536.     case ARP_AX25:
  537.      if(riface->iface->type != CL_AX25)
  538.           return;
  539.      break;
  540.     case ARP_ETHER:
  541.      if(riface->iface->type != CL_ETHERNET)
  542.           return;
  543.      break;
  544.     case NHWTYPES:
  545.      break;        /* "Any interface type is ok" type entry */
  546.     default:
  547.      return;
  548.     }
  549.     if((adj = (struct rspfadj *)calloc(1,sizeof(struct rspfadj)))==NULLADJ)
  550.     return;
  551.     adj->addr = addr;
  552.     if(iface == NULLIF)      /* A manual ARP entry or Request, may be no-good */
  553.     adj->state = RSPF_TENTATIVE;
  554.     else {
  555.     adj->state = RSPF_OK;
  556.     adj->okcnt++;
  557.     adj->timer.func = rspfevent;        /* Fill in timer values */
  558.     adj->timer.arg = (void *) &adj->timer;
  559.     set_timer(&adj->timer,dur_timer(&Susptimer));
  560.     start_timer(&adj->timer);    
  561.     }
  562.     if((bp = alloc_mbuf(1+sizeof(int32)+sizeof(iface))) == NULLBUF) {
  563.      stop_timer(&adj->timer);
  564.      free((char *)adj);
  565.      return;
  566.     }
  567.     *bp->data = RSPFE_ARP;
  568.     memcpy(bp->data + 1, (char *) &adj, sizeof(adj));
  569.     memcpy(bp->data + 1 + sizeof(adj), (char *) &iface, sizeof(iface));
  570.     bp->cnt = bp->size;
  571.     enqueue(&Rspfinq,bp);
  572. }
  573.  
  574. /* Called by "route add" command. */
  575. void
  576. rspfrouteupcall(addr,bits,gateway)
  577. int32 addr;            /* Address added to routing table */
  578. unsigned bits;            /* Significant bits in address */
  579. int32 gateway;            /* Address of gateway station */
  580. {
  581.     /* We are only interested in 32 bit specific routes that use a
  582.      * gateway. Direct routes will be discovered anyway.
  583.      */
  584.     if(bits != 32 || gateway == 0 || gateway == addr)
  585.      return;
  586.     if(rtif_lookup(addr) == NULLRIFACE) /* not routed onto RSPF interface */
  587.      return;
  588.     rspfarpupcall(addr,NHWTYPES,NULLIF); /* proceed as an "arp add" upcall */
  589. }
  590.  
  591. /* When the suspect timer expires, we scan through the routing table for all
  592.  * manual 32 bit specific routes through a gateway that are not an adjacency,
  593.  * and calls rspfrouteupcall(). A similar thing is done for all manual ARP
  594.  * entries. This will make sure that a station that was not reachable when
  595.  * the "route add" or "arp add" command was executed will eventually be
  596.  * discovered if it joins the network.
  597.  */
  598. void
  599. rspfsuspect(t)
  600. void *t;
  601. {
  602.      struct rspfadj *adj;
  603.      struct route *rp;
  604.      struct arp_tab *ap;
  605.      int i;
  606.      start_timer(&Susptimer);            /* restart the timer */
  607.      for(i = 0; i < HASHMOD; i++)        /* Check the routing table */
  608.       for(rp = Routes[31][i]; rp != NULLROUTE; rp = rp->next) {
  609.            if((rp->flags & RTPRIVATE) || dur_timer(&rp->timer) != 0)
  610.             continue;    /* not this route */
  611.            for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  612.             if(adj->addr == rp->target)
  613.              break;
  614.            if(adj == NULLADJ) /* it is not already an adjacency */
  615.             rspfrouteupcall(rp->target,32,rp->gateway);
  616.       }
  617.      for(i=0; i < HASHMOD; ++i)    /* scan the ARP table */
  618.       for(ap = Arp_tab[i]; ap != NULLARP; ap = ap->next) {
  619.            if(dur_timer(&ap->timer))    /* not a manual entry */
  620.             continue;
  621.            for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  622.             if(adj->addr == ap->ip_addr)
  623.              break;
  624.            if(adj == NULLADJ)    /* not already an adjacency */
  625.             rspfarpupcall(ap->ip_addr,ap->hardware,NULLIF);
  626.       }
  627. }
  628.  
  629. /* This non-layered function peeks at the routing table to figure out to which
  630.  * interface datagrams for addr will be routed. Then it returns the matching
  631.  * rspfiface structure.
  632.  */
  633. static
  634. struct rspfiface *
  635. rtif_lookup(addr)
  636. int32 addr;
  637. {
  638.     struct rspfiface *riface;
  639.     struct route *rp;
  640.     if((rp = rt_lookup(addr)) == NULLROUTE)
  641.     return NULLRIFACE;
  642.     riface = Rspfifaces;
  643.     while(riface != NULLRIFACE){
  644.     if(riface->iface == rp->iface)
  645.         return riface;
  646.     riface = riface->next;
  647.     }
  648.     return NULLRIFACE;
  649. }
  650.  
  651. /* Send good or bad news partial routing updates. A cost of 255 should be
  652.  * interpreted as bad news by the remote station.
  653.  */
  654. static void
  655. goodbadnews(adj)
  656. struct rspfadj *adj;
  657. {
  658.     struct rspfnodeh nodeh;
  659.     struct rspflinkh linkh;
  660.     struct rspfiface *riface;
  661.     struct rspfrouter *rr;
  662.     struct mbuf *bp, *tbp, *wbp;
  663.     int nodecnt = 1;
  664.     if((riface = rtif_lookup(adj->addr)) == NULLRIFACE)
  665.     return;
  666.     bp = ambufw(5);
  667.     bp->cnt = bp->size;
  668.     *bp->data = 32;     /* the number of significant bits in the address */
  669.     put32(bp->data+1,adj->addr);
  670.     linkh.horizon = riface->horizon;
  671.     linkh.erp = riface->erp;
  672.     linkh.cost = adj->cost;
  673.     linkh.adjn = 1;
  674.     tbp = htonrspflink(&linkh,bp);
  675.     nodeh.seq = Rspfseq;
  676.     nodeh.subseq = ++Rspfsubseq;
  677.     nodeh.links = 1;
  678.     for(riface = Rspfifaces; riface != NULLRIFACE; riface = riface->next) {
  679.       if(dup_p(&wbp,tbp,0,len_p(tbp)) != len_p(tbp)) {
  680.            free_p(wbp);
  681.            continue;
  682.        }
  683.        nodeh.addr = riface->iface->addr;
  684.        bp = htonrspfnode(&nodeh,wbp);
  685.        sendtoall(bp,1,riface);
  686.     }
  687.     free_p(tbp);
  688.     /* If this is a new adjacency, then send it a routing update immediately */
  689.     if(adj->cost == 255 || adj->okcnt != 1)
  690.     return;
  691.     /* When two RSPF stations learn about each others existence, one of
  692.      * them will usually have received an RRH from the other. The one that
  693.      * received the RRH will send the peer a routing update immediately.
  694.      * So in the code below, if no RRH has been received (seq is 0) and no
  695.      * routing update has yet been received, we should expect to receive a
  696.      * routing update shortly if the adjacency is running RSPF.
  697.      * This could fail though if two RSPF stations learn about each other
  698.      * solely through ARP upcalls and no RRH's or Updates were exchanged
  699.      * prior to or during the establishment of the adjacency.
  700.      */
  701.     if(adj->seq == 0 && rr_lookup(adj->addr) == NULLRROUTER) {
  702.     if(adj->state != RSPF_SUSPECT)    /* running in RSPF process, give up */
  703.         return;
  704.     alarm(15 * 1000);    /* wait for an Update */
  705.     if(pwait(adj) == EALARM)
  706.          return;    /* still no sign of RSPF activity from the adjacency */
  707.     alarm(0);
  708.     }
  709.     ++adj->okcnt;    /* we don't want to get here again */
  710.     if((bp = makeownupdate(adj->addr,1)) == NULLBUF)
  711.     return;
  712.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  713.     if((tbp = makeadjupdate(get32(rr->data->data),rr)) != NULLBUF){
  714.          append(&bp,tbp);
  715.          nodecnt++;
  716.     }
  717.     sendupdate(bp,nodecnt,adj->addr);
  718. }
  719.  
  720. /* This function is called whenever it is time to send a new RSPF update */
  721. static void
  722. sendrspf()
  723. {
  724.     struct rspfrouter *rr;
  725.     struct mbuf *bp, *wbp, *tmp = NULLBUF;
  726.     struct rspfiface *riface;
  727.     struct rspfadj *adj;
  728.     int nodecnt, incr = 1;
  729.  
  730.     for(nodecnt = 1, rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  731.      if(!rr->sent)        /* don't send stale data */
  732.           if((bp = makeadjupdate(get32(rr->data->data),rr)) != NULLBUF){
  733.            append(&tmp,bp);
  734.            nodecnt++;
  735.           }
  736.     for(riface = Rspfifaces; riface != NULLRIFACE; riface = riface->next) {
  737.      /* Find an address that is routed onto this interface */
  738.      for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  739.           if(rtif_lookup(adj->addr) == riface)
  740.            break;
  741.      if(adj == NULLADJ)    /* no adjacency on that interface? */
  742.           continue;
  743.      if(dup_p(&wbp,tmp,0,len_p(tmp)) != len_p(tmp)) {
  744.           free_p(wbp);
  745.           continue;
  746.      }
  747.      if((bp = makeownupdate(adj->addr,incr)) != NULLBUF) {
  748.           incr = 0;    /* sequence number is incremented only once */
  749.           append(&bp,wbp);
  750.      }
  751.      else
  752.           break;
  753.      sendtoall(bp,nodecnt,riface);
  754.     }
  755.     free_p(tmp);
  756.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  757.      if(!rr->sent)        /* Mark router data as propagated */
  758.           ++rr->sent;
  759. }
  760.  
  761. /* This function is used to answer "poll" messages */
  762. static void
  763. sendonerspf(addr,dest)
  764. int32 addr;    /* address of station whose routing update we will make */
  765. int32 dest;    /* address of station to send the update to */
  766. {
  767.     struct mbuf *bp;
  768.     if(ismyaddr(addr)){
  769.     if((bp = makeownupdate(dest,0)) == NULLBUF)
  770.         return;
  771.     sendupdate(bp,1,dest);
  772.     return;
  773.     }
  774.     if((bp = makeadjupdate(addr,NULLRROUTER)) == NULLBUF)
  775.     return;
  776.     sendupdate(bp,1,dest);
  777. }
  778.     
  779. /* send an update to all adjacencies on an RSPF interface */
  780. static void
  781. sendtoall(bp,nodecnt,riface)
  782. struct mbuf *bp;
  783. int nodecnt;            /* number of reporting nodes in update */
  784. struct rspfiface *riface;    /* interface to sent to */
  785. {
  786.      struct rspfadj *adj;
  787.      struct mbuf *wbp;
  788.      int broad;
  789.      for(broad = 0, adj = Adjs; adj != NULLADJ; adj = adj->next)
  790.       /* If adj->seq is 0 we have never received an RRH from the
  791.        * adjacency, and if there is no stored routing update, then
  792.        * it is not known if the adjacency understands RSPF.
  793.        */
  794.       if((adj->seq != 0 || rr_lookup(adj->addr) != NULLRROUTER) &&
  795.          riface == rtif_lookup(adj->addr)) {
  796.            if((adj->tos & RELIABILITY) && !(adj->tos & DELAY)) {
  797.             if(dup_p(&wbp,bp,0,len_p(bp)) != len_p(bp)) {
  798.              free_p(wbp);
  799.              continue;
  800.             }
  801.             sendupdate(wbp,nodecnt,adj->addr); /* private copy */
  802.            }
  803.            else
  804.             ++broad;    /* send to broadcast IP address instead */
  805.       }
  806.      if(broad != 0)
  807.       if(dup_p(&wbp,bp,0,len_p(bp)) != len_p(bp))
  808.            free_p(wbp);
  809.       else
  810.            sendupdate(wbp,nodecnt,riface->iface->broadcast);
  811.      free_p(bp);
  812. }     
  813.  
  814. /* This function sends a routing update to the adjacencies that we have
  815.  * recevied RRH's from.
  816.  */
  817. static int
  818. sendupdate(bp,nodecnt,addr)
  819. struct mbuf *bp;    /* Full packet, except for the packet header */
  820. int nodecnt;        /* Number of reporting nodes in the packet */
  821. int32 addr;        /* Station to send update to */
  822. {
  823.     struct rspfadj *adj;
  824.     struct mbuf *tmp;
  825.     int tos = 0;
  826.  
  827.     /* See if we are sending to a known adjacency, in use its TOS in
  828.      * that case.
  829.      */
  830.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  831.      if(adj->addr == addr) {
  832.           tos = adj->tos;
  833.           break;
  834.      }
  835.     if((tmp = fragmenter(bp,nodecnt,ip_mtu(addr) - IPLEN)) == NULLBUF)
  836.      return -1;
  837.     while((bp = dequeue(&tmp)) != NULLBUF){
  838.      rspfcsum(&bp,addr);    /* Calculate the checksum */
  839.      ++Rspf_stat.updateout;
  840.      ip_send(INADDR_ANY,addr,RSPF_PTCL,INET_CTL | tos,2,bp,0,0,0);
  841.     }
  842.     return 0;
  843. }
  844.  
  845. /* Fragment a large mbuf if necessary into packets of maximum mtu bytes.
  846.  * Each packet is prepended a RSPF routing update header, without the checksum.
  847.  */
  848. static struct mbuf *
  849. fragmenter(bp,nodes,mtu)
  850. struct mbuf *bp; /* packet to send, without packet header */
  851. int nodes;    /* The number of reporting nodes in the routing update */
  852. int16 mtu;    /* The maximum transmission unit */
  853. {
  854.     struct rspfnodeh nodeh;
  855.     struct rspflinkh linkh;
  856.     struct rspfpacketh pkth;
  857.     struct mbuf *tbp, *tmp, *bpq = NULLBUF;
  858.     int n, nodecnt, linkcnt, adjcnt, nodemax, sync;
  859.     char *cp, fragn = 1;
  860.  
  861.     if(mtu < RSPFPKTLEN + RSPFNODELEN || bp == NULLBUF) {
  862.      free_p(bp);
  863.      return NULLBUF;
  864.     }
  865.     ++Envelopeid;
  866.     nodemax = nodes;        /* total number of nodes in envelope */
  867.     nodecnt = nodes;        /* nodes left to packetize */
  868.     linkcnt = adjcnt = 0;
  869. #ifdef __GNUC__
  870.     cp = 0;            /* silence warning */
  871. #endif
  872.     while(len_p(bp) != 0){
  873.     n = min(mtu,len_p(bp)+RSPFPKTLEN);    /* length of this packet */
  874.     n -= RSPFPKTLEN;
  875.     tbp = NULLBUF;
  876.     sync = 0;
  877.     if(adjcnt){
  878.         tbp = ambufw(min(adjcnt*5,n/5*5));
  879.         tbp->cnt = tbp->size;
  880.         cp = tbp->data;
  881.     }
  882.     while(n > 0){
  883.         while(adjcnt){
  884.         if((n -= 5) < 0)
  885.             break;
  886.         pullup(&bp,cp,5);
  887.         cp += 5;
  888.         adjcnt--;
  889.         }
  890.         if(linkcnt && n > 0){
  891.         if((n -= RSPFLINKLEN) < 0)
  892.             break;
  893.         ntohrspflink(&linkh,&bp);
  894.         adjcnt = linkh.adjn;
  895.         tmp = htonrspflink(&linkh,NULLBUF);
  896.         append(&tbp,tmp);
  897.         tmp = ambufw(min(5*adjcnt,n/5*5));
  898.         tmp->cnt = tmp->size;
  899.         cp = tmp->data;
  900.         append(&tbp,tmp);
  901.         linkcnt--;
  902.         continue;
  903.         }
  904.         if(nodecnt && linkcnt == 0 && n > 0){
  905.         if((n -= RSPFNODELEN) < 0)
  906.             break;
  907.         if(nodecnt == nodes)        /* Set the sync octet */
  908.             sync = len_p(tbp) + 4;
  909.         ntohrspfnode(&nodeh,&bp);
  910.         linkcnt = nodeh.links;
  911.         tmp = htonrspfnode(&nodeh,NULLBUF);
  912.         append(&tbp,tmp);
  913.         nodecnt--;
  914.         }
  915.     }
  916.     pkth.version = RSPF_VERSION;    /* The version number */
  917.     pkth.type = RSPF_FULLPKT;    /* The packet type */
  918.     pkth.fragn = fragn++;        /* The fragment number */
  919.     pkth.fragtot = 0;        /* The total number of fragments */
  920.     pkth.csum = 0;            /* The checksum */
  921.     pkth.sync = sync < 256 ? sync : 0;    /* The sync octet */
  922.     pkth.nodes = nodemax;        /* The number of nodes in envelope */
  923.     pkth.envid = Envelopeid;    /* The envelope-ID */
  924.     tmp = htonrspf(&pkth,tbp);    /* add packet header */
  925.     enqueue(&bpq,tmp);        /* add packet to chain */
  926.     nodes = nodecnt;        /* number of nodes left */
  927.     }
  928.     free_p(bp);
  929.     for(tbp = bpq; tbp != NULLBUF; tbp = tbp->anext)
  930.     *(tbp->data + 3) = len_q(bpq);    /* Set the fragment total counter */
  931.     return bpq;
  932. }
  933.  
  934. /* Calculate the checksum on an RSPF routing update packet */
  935. static void
  936. rspfcsum(bpp,dest)
  937. struct mbuf **bpp;
  938. int32 dest;
  939. {
  940.     struct pseudo_header ph;
  941.     int16 checksum;
  942.     ph.length = len_p(*bpp);
  943.     ph.source = locaddr(dest);
  944.     ph.dest = dest;
  945.     ph.protocol = RSPF_PTCL;
  946.     if((checksum = cksum(&ph,*bpp,ph.length)) == 0)
  947.     checksum = 0xffffffff;
  948.     /* This assumes that the checksum really is at this position */
  949.     put16((*bpp)->data+4,checksum);
  950. }
  951.  
  952. /* This function creates our own routing update and returns it in an mbuf */
  953. struct mbuf *
  954. makeownupdate(dest,new)
  955. int32 dest;        /* Address of a station that will get this update */
  956. int new;        /* True if the sequence number should be incremented */
  957. {
  958.     struct rspfadj *adj;
  959.     struct rspflinkh linkh;
  960.     struct rspfnodeh nodeh;
  961.     struct rspfiface *riface, rifdefault;
  962.     struct mbuf *bp = NULLBUF, *tbp, *tmp;
  963.     struct route *rp;
  964.     int i, adjcnt, lnkcnt, bits;
  965.     int32 prev, low, cur;
  966.  
  967.     /* Set default values to apply to non-RSPF interfaces */
  968.     rifdefault.horizon = 32;
  969.     rifdefault.erp = 0;
  970.     rifdefault.quality = 0;
  971.     /* Our adjacencies has to be sorted into groups of the same horizon,
  972.      * ERP factor and cost. This is done by combining these numbers into a
  973.      * single one, modulo 256 and take the lowest number first.
  974.      */
  975.     low = 0;
  976.     lnkcnt = 0;
  977.     for(;;){
  978.     prev = low;
  979.     low = 255*65536L+255*256+255;
  980.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  981.          /* Include all adjacecies that have been or are in OK state
  982.           * in the update. Bad adjacencies are also included to give
  983.           * them a chance to recover. Hopelessly Bad adjacencies are
  984.           * eventually deleted elsewhere.
  985.           */
  986.         if(adj->okcnt != 0 && (riface = rtif_lookup(adj->addr)) !=
  987.            NULLRIFACE) {
  988.         cur = riface->horizon*65536L+riface->erp*256+adj->cost;
  989.         if(cur > prev && cur < low)
  990.             low = cur;
  991.         }
  992.     /* Add any manual public, 1-31 bit specific routes.
  993.      * Use the route metric only if it is greater than the default
  994.      * quality to lessen a possible "wormhole" effect.
  995.      */
  996.     for(bits = 1; bits <= 31; bits++)
  997.         for(i = 0; i < HASHMOD; i++)
  998.         for(rp = Routes[bits-1][i]; rp != NULLROUTE; rp = rp->next)
  999.              if(!(rp->flags & RTPRIVATE) && !dur_timer(&rp->timer)) {
  1000.              if((riface = rtif_lookup(rp->target)) == NULLRIFACE)
  1001.                     riface = &rifdefault;
  1002.              cur = riface->horizon*65536L+riface->erp*256+
  1003.                 (rp->metric > riface->quality ? rp->metric :
  1004.                 riface->quality);
  1005.             if(cur > prev && cur < low)
  1006.                 low = cur;
  1007.              }
  1008.     /* Add any 32 bit routes on interfaces not using RSPF */
  1009.     for(i = 0; i < HASHMOD; i++)
  1010.          for(rp = Routes[31][i]; rp != NULLROUTE; rp = rp->next)
  1011.           if(!(rp->flags & RTPRIVATE) && rtif_lookup(rp->target)
  1012.              == NULLRIFACE) {
  1013.                cur = rifdefault.horizon*65536L+rifdefault.erp*256+
  1014.                 (rp->metric > rifdefault.quality ? rp->metric :
  1015.                  rifdefault.quality);
  1016.                if(cur > prev && cur < low)
  1017.                 low = cur;
  1018.           }
  1019.     /* Add the default route */
  1020.     if((rp = rt_blookup(0,0)) != NULLROUTE && !(rp->flags & RTPRIVATE) &&
  1021.        !dur_timer(&rp->timer)) {
  1022.          if((riface = rtif_lookup(0)) == NULLRIFACE)
  1023.           riface = &rifdefault;
  1024.          cur = riface->horizon*65536L+riface->erp*256+
  1025.         (rp->metric > riface->quality ? rp->metric : riface->quality);
  1026.          if(cur > prev && cur < low)
  1027.           low = cur;
  1028.     }
  1029.     if(low == 255*65536L+255*256+255) /* All done */
  1030.         break;
  1031.     /* now start adding the routes */
  1032.     adjcnt = 0;
  1033.     tbp = NULLBUF;
  1034.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  1035.         if(adj->okcnt != 0 && (riface = rtif_lookup(adj->addr)) !=
  1036.            NULLRIFACE)
  1037.         if(riface->horizon*65536L+riface->erp*256+adj->cost == low){
  1038.             pushadj(&tbp,adj->addr,32);
  1039.             adjcnt++;
  1040.         }
  1041.     for(bits = 1; bits <= 31; bits++)
  1042.         for(i = 0; i < HASHMOD; i++)
  1043.         for(rp = Routes[bits-1][i]; rp != NULLROUTE; rp = rp->next)
  1044.              if(!(rp->flags & RTPRIVATE) && !dur_timer(&rp->timer)){
  1045.             if((riface = rtif_lookup(rp->target)) == NULLRIFACE)
  1046.                    riface = &rifdefault;
  1047.             /* Manually entered local routes only */
  1048.             if(riface->horizon*65536L+riface->erp*256+
  1049.                (rp->metric > riface->quality ? rp->metric :
  1050.                riface->quality) == low){
  1051.                 pushadj(&tbp,rp->target,bits);
  1052.                 adjcnt++;
  1053.             }
  1054.              }
  1055.     for(i = 0; i < HASHMOD; i++)    /* 32 bit specific routes */
  1056.          for(rp = Routes[31][i]; rp != NULLROUTE; rp = rp->next)
  1057.           if(!(rp->flags & RTPRIVATE) && rtif_lookup(rp->target)
  1058.              == NULLRIFACE)
  1059.                if(rifdefault.horizon*65536L+rifdefault.erp*256+
  1060.               (rp->metric > rifdefault.quality ? rp->metric :
  1061.                rifdefault.quality) == low){
  1062.                 pushadj(&tbp,rp->target,bits);
  1063.                 adjcnt++;
  1064.                }
  1065.     /* Add the default route */
  1066.     if((rp = rt_blookup(0,0)) != NULLROUTE && !(rp->flags & RTPRIVATE) &&
  1067.        !dur_timer(&rp->timer)) {
  1068.          if((riface = rtif_lookup(0)) == NULLRIFACE)
  1069.           riface = &rifdefault;
  1070.          if(riface->horizon*65536L+riface->erp*256+ (rp->metric > 
  1071.         riface->quality ? rp->metric : riface->quality) == low){
  1072.           pushadj(&tbp,0,0);
  1073.           adjcnt++;
  1074.          }
  1075.     }
  1076.     if(adjcnt != 0){
  1077.         /* Prepend the link header */
  1078.         linkh.horizon = ((low >> 16) & 255);/* Horizon value */
  1079.         linkh.erp = ((low >> 8) & 255);    /* ERP factor */
  1080.         linkh.cost = (low & 255);        /* Link cost */
  1081.         linkh.adjn = adjcnt;        /* Number of adjacencies */
  1082.         tmp = htonrspflink(&linkh,tbp);
  1083.         append(&bp,tmp);
  1084.         lnkcnt++;
  1085.     }
  1086.     }
  1087.     /* Prepend the node header */
  1088.     if(lnkcnt != 0){
  1089.     /* Set our address to the IP address used on the destinations
  1090.      * interface.
  1091.      */
  1092.     if(dest == INADDR_ANY || (riface = rtif_lookup(dest)) == NULLRIFACE)
  1093.          nodeh.addr = Ip_addr;
  1094.     else
  1095.          nodeh.addr = riface->iface->addr;
  1096.     if(new){    /* increase sequence number, clear subseq. number */
  1097.         if(Rspfseq == 32768 - 2 || Rspfseq == -32768 + 1)
  1098.          Rspfseq = 0;            /* wrap around */
  1099.         else
  1100.          ++Rspfseq;
  1101.         Rspfsubseq = 0;
  1102.     }
  1103.     nodeh.seq = Rspfseq;
  1104.     nodeh.subseq = 0;
  1105.     nodeh.links = lnkcnt;
  1106.     return htonrspfnode(&nodeh,bp);
  1107.     }
  1108.     else {
  1109.     free_p(bp);
  1110.     return NULLBUF;
  1111.     }
  1112. }
  1113. /* Prepends an adjacency to bpp. Allocates bpp in large chunk for efficency */
  1114. static void
  1115. pushadj(bpp,addr,bits)
  1116. struct mbuf **bpp;
  1117. int32 addr;
  1118. int bits;
  1119. {
  1120.      if(bpp == NULLBUFP)
  1121.       return;
  1122.      if(*bpp == NULLBUF) {
  1123.       *bpp = ambufw(60);        /* good for 12 adjacencies */
  1124.       (*bpp)->data += 55;
  1125.       (*bpp)->cnt = 5;
  1126.      }
  1127.      else
  1128.       *bpp = pushdown(*bpp,5);
  1129.      *(*bpp)->data = bits;
  1130.      put32((*bpp)->data+1,addr);
  1131. }
  1132.  
  1133. /* This function returns the latest routing update in network fromat from
  1134.  * the adjacency denoted by addr.
  1135.  */
  1136. static struct mbuf *
  1137. makeadjupdate(addr,rr)
  1138. int32 addr;
  1139. struct rspfrouter *rr;    /* pointer to stored routing entry, if known */
  1140. {
  1141.     struct mbuf *tmp, *tmp2, *tmp3, *bp = NULLBUF;
  1142.     struct rspfnodeh nodeh;
  1143.     struct rspflinkh linkh;
  1144.     int linkcnt = 0;
  1145.     if(rr == NULLRROUTER && (rr = rr_lookup(addr)) == NULLRROUTER)
  1146.     return NULLBUF;
  1147.     if(dup_p(&tmp,rr->data,0,len_p(rr->data)) != len_p(rr->data)) {
  1148.      free_p(tmp);
  1149.      return NULLBUF;
  1150.     }
  1151.     ntohrspfnode(&nodeh,&tmp);             /* Get the node header */
  1152.     while(nodeh.links--){
  1153.     ntohrspflink(&linkh,&tmp);
  1154.     /* Decrement and check the horizon value */
  1155.     if(--linkh.horizon > 0){
  1156.         /* Duplicate the adjacencies */
  1157.         if(dup_p(&tmp2,tmp,0,5*linkh.adjn) != 5*linkh.adjn){
  1158.         free_p(tmp);
  1159.         free_p(tmp2);
  1160.         free_p(bp);
  1161.         return NULLBUF;
  1162.         }
  1163.         /* Prepend the link header */
  1164.         tmp3 = htonrspflink(&linkh,tmp2);
  1165.         append(&bp,tmp3);
  1166.         linkcnt++;
  1167.     }
  1168.     pullup(&tmp,NULLCHAR,5*linkh.adjn); /* Skip the adjacencies */
  1169.     }
  1170.     free_p(tmp);
  1171.     if(linkcnt == 0){         /* No links at all? */
  1172.     free_p(bp);
  1173.     return NULLBUF;
  1174.     }
  1175.     nodeh.subseq = rr->subseq;
  1176.     nodeh.links = linkcnt;
  1177.     /* Prepend the node header */
  1178.     return htonrspfnode(&nodeh,bp);
  1179. }
  1180.  
  1181. /* Returns 1 if sequence number a is newer than b. Sequence numbers start
  1182.  * at -32768 + 1 and then continues with 0 and the positive integer numbers
  1183.  * until reaching 32768 - 2 after which they continue with 0.
  1184.  * Algorithm taken from RFC-1131 but fixed bug when a or b is 0.
  1185.  */
  1186. static int
  1187. isnewer(a,b)
  1188. short a,b;
  1189. {
  1190.      if((b < 0 && a > b) ||
  1191.     (a >= 0 && b >= 0 &&    /* bug corrected here */
  1192.      (((32768 - 1)/2 > (a-b) && (a-b) > 0) ||
  1193.       (a-b) < -(32768 - 1)/2)))
  1194.       return 1;
  1195.      return 0;
  1196. }
  1197.      
  1198. /* Reassemble possibly fragmented packets into a single large one */
  1199. static struct mbuf *
  1200. rspfreasm(bp,rph,addr)
  1201. struct mbuf *bp;      /* Routing update packet without packet header */
  1202. struct rspfpacketh *rph;  /* Packet header */
  1203. int32 addr;          /* Address of station we got the packet from */
  1204. {
  1205.     union rspf rspf;
  1206.     struct rspfreasm *re, *rtmp;
  1207.     struct mbuf *tbp, *bp1, *bp2;
  1208.     for(re = Rspfreasmq; re != NULLRREASM; re = re->next)
  1209.     if(re->addr == addr){    /* found another fragment */
  1210.         if(dup_p(&tbp,re->data,0,RSPFPKTLEN) != RSPFPKTLEN) {
  1211.          free_p(tbp);
  1212.          free_p(bp);
  1213.          return NULLBUF;
  1214.         }
  1215.         ntohrspf(&rspf,&tbp);    /* get its packet header */
  1216.         if(rph->envid != rspf.pkthdr.envid) {
  1217.          del_reasm(re);        /* an obsolete entry, delete it */
  1218.          break;
  1219.         }
  1220.         /* Now try to find a place where this fragment fits in the chain */
  1221.         bp1 = re->data;
  1222.         bp2 = NULLBUF;
  1223.         while(rph->fragn > rspf.pkthdr.fragn && bp1->anext != NULLBUF){
  1224.         bp2 = bp1;
  1225.         bp1 = bp1->anext;
  1226.         if(dup_p(&tbp,bp1,0,RSPFPKTLEN) != RSPFPKTLEN) {
  1227.              free_p(bp);
  1228.              free_p(tbp);
  1229.              return NULLBUF;
  1230.         }
  1231.         ntohrspf(&rspf,&tbp);
  1232.         }
  1233.         if(rspf.pkthdr.fragn == rph->fragn) {
  1234.          free_p(bp);
  1235.          return NULLBUF;    /* We already had this one */
  1236.         }
  1237.         bp = htonrspf(rph,bp);    /* Put the packet header back on */
  1238.         /* Insert the fragment at the right place in the chain */
  1239.         if(rph->fragn > rspf.pkthdr.fragn){
  1240.         bp1->anext = bp;
  1241.         bp->anext = NULLBUF;
  1242.         }
  1243.         else {
  1244.         if(bp2 != NULLBUF)
  1245.             bp2->anext = bp;
  1246.         else
  1247.             re->data = bp;
  1248.         bp->anext = bp1;
  1249.         }
  1250.         if(len_q(re->data) == rspf.pkthdr.fragtot){ 
  1251.         bp1 = NULLBUF;            /* we have all the fragments */
  1252.         while((bp2 = dequeue(&re->data)) != NULLBUF){
  1253.             pullup(&bp2,NULLCHAR,RSPFPKTLEN); /* strip the headers */
  1254.             append(&bp1,bp2);        /* and form a single packet */
  1255.         }
  1256.         del_reasm(re);
  1257.         stop_timer(&Rspfreasmt);
  1258.         reasmtimeout(NULL);    /* restarts timer if necessary */
  1259.         return bp1;        /* return the completed packet */
  1260.         }
  1261.         re->time = secclock();        /* Update the timestamp */
  1262.         goto timstart;        /* We have to wait for fragments */
  1263.     }
  1264.     /* At this point we know that there is no matching entry in the
  1265.        reassembly queue (any more) */
  1266.     if(rph->fragtot == 1)    /* Simple case, an un-fragmented packet */
  1267.     return bp;
  1268.     tbp = htonrspf(rph,bp);    /* Put the packet header back on */
  1269.     rtmp = (struct rspfreasm *) callocw(1,sizeof(struct rspfreasm));
  1270.     /* The values are filled in */
  1271.     rtmp->addr = addr;
  1272.     rtmp->time = secclock();
  1273.     rtmp->data = tbp;
  1274.     rtmp->next = Rspfreasmq;
  1275.     Rspfreasmq = rtmp;
  1276.  timstart:                    /* Start the timer if needed */
  1277.     if(!run_timer(&Rspfreasmt)){
  1278.     Rspfreasmt.func = reasmtimeout;
  1279.     Rspfreasmt.arg = NULL;
  1280.     set_timer(&Rspfreasmt,RSPF_RTIME*1000L); /* The reassembly timeout */
  1281.     start_timer(&Rspfreasmt);
  1282.     }
  1283.     return NULLBUF;
  1284. }
  1285.  
  1286. /* Delete a reassembly descriptor from the queue */
  1287. static void
  1288. del_reasm(re)
  1289. struct rspfreasm *re;
  1290. {
  1291.     struct rspfreasm *r, *prev = NULLRREASM;
  1292.     for(r = Rspfreasmq; r != NULLRREASM; prev = r, r = r->next)
  1293.     if(r == re){
  1294.         free_q(&re->data);
  1295.         if(prev != NULLRREASM)
  1296.         prev->next = re->next;
  1297.         else
  1298.         Rspfreasmq = re->next;
  1299.         free((char *)re);
  1300.         break;
  1301.     }
  1302. }
  1303.  
  1304. /* When the reassembly timer times out, this function tries to make use of
  1305.  * any fragments in the reassembly queue.
  1306.  */
  1307. static void
  1308. reasmtimeout(t)
  1309. void *t;
  1310. {
  1311.     union rspf rspf;
  1312.     struct rspfreasm *re;
  1313.     struct mbuf *bp, *tbp;
  1314.     int last = 0;
  1315.     int32 low;
  1316.     re = Rspfreasmq;
  1317.     while(re != NULLRREASM)
  1318.     if((secclock() - re->time) >= RSPF_RTIME){
  1319.         /* Try to use what we have */
  1320.         bp = NULLBUF;
  1321.         while((tbp = dequeue(&re->data)) != NULLBUF){
  1322.         ntohrspf(&rspf,&tbp);
  1323.         if(rspf.pkthdr.fragn != (last+1)){  /* a missing fragment */
  1324.             if(bp != NULLBUF)
  1325.             update_in(bp,re->addr);
  1326.             bp = NULLBUF;
  1327.             if(rspf.pkthdr.sync != 0)
  1328.             pullup(&tbp,NULLCHAR,rspf.pkthdr.sync - 4);
  1329.             else {    /* no sync possible in this fragment */
  1330.              free_p(tbp);
  1331.              continue;
  1332.             }
  1333.         }
  1334.         append(&bp,tbp);
  1335.         last = rspf.pkthdr.fragn;
  1336.         }
  1337.         if(bp != NULLBUF)
  1338.         update_in(bp,re->addr);
  1339.         del_reasm(re);
  1340.         re = Rspfreasmq;    /* start over again */
  1341.         }
  1342.     else
  1343.          re = re->next;
  1344.     /* Find the oldest fragment and restart the reassembly timer */
  1345.     low = 0;
  1346.     for(re = Rspfreasmq; re != NULLRREASM; re = re->next)
  1347.     if(re->time < low || low == 0)
  1348.         low = re->time;
  1349.     if(low){
  1350.     /* KF8NH 1993.01.17: clock() on below line changed to secclock() */
  1351.     set_timer(&Rspfreasmt,RSPF_RTIME*1000L - secclock() + low);
  1352.     if(dur_timer(&Rspfreasmt) > 0) /* just to be safe */
  1353.         start_timer(&Rspfreasmt);
  1354.     }
  1355. }
  1356.  
  1357. /* Handle incoming routing updates (a reassembled envelope) */
  1358. static void
  1359. update_in(bp,addr)
  1360. struct mbuf *bp;    /* Reassembled data packet, without packet header */
  1361. int32 addr;        /* Senders address (in host order) */
  1362. {
  1363.     struct rspfnodeh nodeh;
  1364.     struct rspflinkh linkh;
  1365.     struct rspfadj *adj;
  1366.     struct mbuf *tbp, *tmp, *b;
  1367.     int linkcnt = 0, adjcnt = 0;
  1368.     int16 offset = 0;
  1369.     tbp = b = NULLBUF;
  1370.     /* Check to see if the sender is an adjacency */
  1371.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  1372.      if(adj->addr == addr)
  1373.           break;
  1374.     /* One may argue that updates from non-adjacencies should not be
  1375.      * accepted since they will not contribute to the routing table.
  1376.      * But it might happen that the sender will very shortly become an
  1377.      * adjacency, and then its routing update will come handy. By increasing
  1378.      * Keeprouter, routing updates from disjoint routers will not be not be
  1379.      * purged when makeroutes() is called this time.
  1380.      */
  1381.     if(adj == NULLADJ) {
  1382.      ++Rspf_stat.noadjupdate;
  1383.      Keeprouter += 2;
  1384.     }
  1385.     else
  1386.     psignal(adj,1);        /* alert anyone waiting for the update */
  1387.     while(offset < len_p(bp)){
  1388.     if(adjcnt){
  1389.         if(dup_p(&tmp,bp,offset,5) == 5){
  1390.         append(&tbp,tmp);
  1391.         offset += 5;
  1392.         adjcnt--;
  1393.         continue;
  1394.         }
  1395.         else break;
  1396.     }
  1397.     if(tbp != NULLBUF){
  1398.         tmp = htonrspflink(&linkh,tbp);
  1399.         append(&b,tmp);
  1400.         tbp = NULLBUF;
  1401.     }
  1402.     if(linkcnt){
  1403.         if(dup_p(&tbp,bp,offset,RSPFLINKLEN) == RSPFLINKLEN){
  1404.         ntohrspflink(&linkh,&tbp);
  1405.         adjcnt = linkh.adjn;
  1406.         offset += RSPFLINKLEN;
  1407.         linkcnt--;
  1408.         continue;
  1409.         }
  1410.         else
  1411.         break;
  1412.     }
  1413.     if(b != NULLBUF){
  1414.         tmp = htonrspfnode(&nodeh,b);
  1415.         node_in(tmp,addr,1);     /* a full router update */
  1416.         b = NULLBUF;
  1417.     }
  1418.     if(dup_p(&tmp,bp,offset,RSPFNODELEN) == RSPFNODELEN){
  1419.         ntohrspfnode(&nodeh,&tmp);
  1420.         linkcnt = nodeh.links;
  1421.         offset += RSPFNODELEN;
  1422.     }
  1423.     else {
  1424.          free_p(bp);
  1425.          free_p(tmp);
  1426.          return;
  1427.     }
  1428.     }
  1429.     if(tbp != NULLBUF){
  1430.     /* adjust the adjacency counter in the link header */
  1431.     linkh.adjn -= adjcnt;
  1432.     tmp = htonrspflink(&linkh,tbp);
  1433.     append(&b,tmp);
  1434.     }
  1435.     /* adjust the link counter in the node header */
  1436.     nodeh.links -= linkcnt;
  1437.     tmp = htonrspfnode(&nodeh,b);
  1438.     free_p(bp);
  1439.     if(linkcnt || adjcnt)
  1440.     node_in(tmp,addr,0);     /* a partial router update */
  1441.     else
  1442.     node_in(tmp,addr,1);
  1443. }
  1444.     
  1445. static void
  1446. node_in(bp,addr,full)
  1447. struct mbuf *bp;    /* A single update, starting with the node header */
  1448. int32 addr;        /* Address of station we got packet from */
  1449. int full;        /* False if we got a partial update */
  1450. {
  1451.     struct mbuf *tbp;
  1452.     struct rspfnodeh nodeh, rnodeh;
  1453.     struct rspfrouter *rr;
  1454.     if(dup_p(&tbp,bp,0,RSPFNODELEN) != RSPFNODELEN) {
  1455.      free_p(bp);
  1456.      free_p(tbp);
  1457.      return;
  1458.     }
  1459.     ntohrspfnode(&nodeh,&tbp);
  1460.     if(ismyaddr(nodeh.addr)){
  1461.     /* If another station thinks our routing update sequence number is
  1462.      * larger than it really is, this might be because we have had
  1463.      * a fast system reset and routing updates from the old "epoch"
  1464.      * are still present in the network.
  1465.      */
  1466.     if(isnewer(nodeh.seq,Rspfseq)) {
  1467.         Rspfseq = nodeh.seq + 1;              /* Catch up */
  1468.         if(nodeh.seq == 32768 - 2 || nodeh.seq == -32768 + 1)
  1469.          Rspfseq = 0;                /* Wrap around */
  1470.         sendonerspf(nodeh.addr,addr); /* Send him the latest packet */
  1471.     }
  1472.     free_p(bp);         /* We already know our own adjacencies! */
  1473.     return;
  1474.     }
  1475.     if((rr = rr_lookup(nodeh.addr)) != NULLRROUTER) {
  1476.     if(dup_p(&tbp,rr->data,0,RSPFNODELEN) != RSPFNODELEN) {
  1477.         free_p(bp);
  1478.         free_p(tbp);
  1479.         return;
  1480.         }
  1481.     ntohrspfnode(&rnodeh,&tbp);
  1482.     if(nodeh.seq == rnodeh.seq && nodeh.subseq <= (unsigned) rr->subseq){
  1483.         free_p(bp);
  1484.         return;    /*  We already have this one */
  1485.     }
  1486.     if(isnewer(rnodeh.seq,nodeh.seq)){
  1487.         /* Send him the latest packet */
  1488.         sendonerspf(nodeh.addr,addr);
  1489.         free_p(bp);
  1490.         ++Rspf_stat.oldreport;
  1491.         return;
  1492.     }
  1493.     if(nodeh.subseq > (unsigned) rr->subseq && nodeh.seq == rnodeh.seq){
  1494.         rr->subseq = nodeh.subseq;
  1495.         partialupdate(rr,bp);
  1496.         makeroutes();
  1497.         return;
  1498.     }
  1499.     if(isnewer(nodeh.seq,rnodeh.seq) && !full){
  1500.         partialupdate(rr,bp);
  1501.         makeroutes();
  1502.         /* Send a "poll" packet */
  1503.         --nodeh.seq;
  1504.         nodeh.subseq = 0;
  1505.         nodeh.links = 0;
  1506.         tbp = htonrspfnode(&nodeh,NULLBUF);
  1507.         sendupdate(tbp,1,addr);
  1508.         ++Rspf_stat.outpolls;
  1509.         return;
  1510.     }
  1511.     }
  1512.     else {
  1513.     rr = (struct rspfrouter *) callocw(1,sizeof(struct rspfrouter));
  1514.     rr->next = Rspfrouters;
  1515.     Rspfrouters = rr;
  1516.     }
  1517.     free_p(rr->data);
  1518.     rr->data = bp;
  1519.     rr->subseq = nodeh.subseq;
  1520.     rr->time = secclock();
  1521.     rr->sent = 0;
  1522.     makeroutes();
  1523.     return;
  1524. }
  1525.  
  1526. /* Return the matching RSPF route entry for addr (in host order) */
  1527. static struct rspfrouter *
  1528. rr_lookup(addr)
  1529. int32 addr;
  1530. {
  1531.     struct rspfrouter *rr;
  1532.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  1533.     /* this assumes that the first word of the header is the address */
  1534.     if(rr->data != NULLBUF && get32(rr->data->data) == addr)
  1535.         return rr;
  1536.     return NULLRROUTER;
  1537. }
  1538.  
  1539. /* Delete the route entry for address addr */
  1540. static void
  1541. rr_delete(addr)
  1542. int32 addr;
  1543. {
  1544.     struct rspfrouter *rr, *prev = NULLRROUTER;
  1545.     for(rr = Rspfrouters; rr != NULLRROUTER; prev = rr, rr = rr->next)
  1546.     /* this assumes that the first word of the header is the address */
  1547.     if(rr->data != NULLBUF && get32(rr->data->data) == addr) {
  1548.          if(prev != NULLRROUTER)
  1549.           prev->next = rr->next;
  1550.          else
  1551.           Rspfrouters = rr->next;
  1552.          free_p(rr->data);
  1553.          free((char *)rr);
  1554.          return;
  1555.     }
  1556. }
  1557.  
  1558. /* Handle incoming partial routing updates. Adjacencies from bp will be
  1559.  * merged into rr->data
  1560.  */
  1561. static void
  1562. partialupdate(rr,bp)
  1563. struct rspfrouter *rr;     /* current node entry in routing table */
  1564. struct mbuf *bp;    /* data packet, starting with node header */
  1565. {
  1566.     struct rspflinkh linkh, rlinkh;
  1567.     struct rspfnodeh rnodeh;
  1568.     struct mbuf *wbp, *tbp, *tmp, *b;
  1569.     int adjcnt = 0, radjcnt, rlinkcnt = 0, bits, rbits, added = 0;
  1570.     int32 addr, raddr;
  1571.  
  1572.     rlinkh.adjn = 0;
  1573.     rr->time = secclock();
  1574.     rr->sent = 0;
  1575.     /* Make a working copy of the stored routing update */
  1576.     if(dup_p(&wbp,rr->data,0,len_p(rr->data)) != len_p(rr->data)) {
  1577.      free_p(wbp);
  1578.      free_p(bp);
  1579.      return;
  1580.     }
  1581.     ntohrspfnode(&rnodeh,&wbp);
  1582.     pullup(&bp,NULLCHAR,RSPFNODELEN);    /* Strip off the node header */
  1583.     while(len_p(bp) > 0)
  1584.     if(adjcnt == 0) {
  1585.          if(ntohrspflink(&linkh,&bp) == -1) {
  1586.           free_p(wbp);
  1587.           free_p(bp);
  1588.           return;
  1589.          }
  1590.          adjcnt = linkh.adjn;
  1591.     }
  1592.     else {
  1593.         bits = PULLCHAR(&bp);    /* get one adjacency to merge */
  1594.         if(pullup(&bp,(char *)&addr,4) != 4) {
  1595.          free_p(wbp);
  1596.          free_p(bp);
  1597.          return;
  1598.         }
  1599.         addr = get32((char *)&addr); /* convert to host order */
  1600.         --adjcnt;
  1601.         radjcnt = 0;
  1602.         b = tbp = NULLBUF;
  1603.         for(;;) {            /* search through stored update */
  1604.         if(radjcnt == 0 || len_p(wbp) == 0) {
  1605.              if(tbp != NULLBUF){
  1606.               rlinkh.adjn -= radjcnt;
  1607.               tmp = htonrspflink(&rlinkh,tbp);
  1608.               ++rlinkcnt;
  1609.               append(&b,tmp);
  1610.               tbp = NULLBUF;
  1611.              }
  1612.              if(len_p(wbp) == 0)
  1613.               break;
  1614.         }
  1615.         if(radjcnt == 0) {
  1616.              ntohrspflink(&rlinkh,&wbp);
  1617.              radjcnt = rlinkh.adjn;
  1618.              if(rlinkh.horizon == linkh.horizon && rlinkh.cost ==
  1619.             linkh.cost && rlinkh.erp == linkh.erp) {
  1620.               pushadj(&tbp,addr,bits);
  1621.               ++rlinkh.adjn;
  1622.               added = 1;
  1623.              }
  1624.         }
  1625.         else {
  1626.             rbits = PULLCHAR(&wbp);
  1627.             raddr = pull32(&wbp);
  1628.             --radjcnt;
  1629.             if(bits != rbits || addr != raddr)
  1630.             pushadj(&tbp,raddr,rbits);    /* Put it back */
  1631.             else
  1632.              --rlinkh.adjn;    /* deleted one adjacency */
  1633.             }
  1634.         }
  1635.         wbp = b;    /* wbp now keeps link headers and adjacencies */
  1636.         if(linkh.cost < 255 && !added){ /* Append the new link */
  1637.         ++rnodeh.links;         /* We will add one extra link */
  1638.         tmp = ambufw(5);
  1639.         *tmp->data = bits;
  1640.         put32(tmp->data+1,addr);
  1641.         tmp->cnt = tmp->size;
  1642.         tbp = htonrspflink(&linkh,tmp);
  1643.         append(&wbp,tbp);
  1644.         }
  1645.         added = 0;
  1646.         }
  1647.    free_p(rr->data);
  1648.    rnodeh.links = rlinkcnt;
  1649.    rr->data = htonrspfnode(&rnodeh,wbp);
  1650. }
  1651.  
  1652. /* The shortest path first algorithm */
  1653. static void
  1654. makeroutes()
  1655. {
  1656.     int bits, i, low, adjcnt;
  1657.     struct mbuf *bp;
  1658.     struct route *rp, *rp2, *saved[HASHMOD];
  1659.     struct rspfadj *adj, *lowadj, *gateway;
  1660.     char *lowp, *r;
  1661.     int32 lowaddr;
  1662.     struct rspflinkh linkh;
  1663.     struct rspfrouter *rr, *rrprev;
  1664.  
  1665. #ifdef __GNUC__
  1666.     gateway = 0;        /* avoid "uninitialized" warning */
  1667. #endif
  1668.     if(Keeprouter)    /* if false, purge unreachable router entries */
  1669.      --Keeprouter;
  1670.     /* Before we change anything in the routing table, we have to save
  1671.      * each local adjacencies riface pointer
  1672.      */
  1673.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  1674.     adj->scratch = (void *) rtif_lookup(adj->addr);
  1675.     /* Remove all non-manual routes */
  1676.     for(bits = 1; bits <= 32; bits++)
  1677.     for(i = 0; i < HASHMOD; i++)
  1678.         for(rp = Routes[bits-1][i]; rp != NULLROUTE; rp = rp2) {
  1679.             rp2 = rp->next;         /* BIG BUG FIX -- sm6rpz */
  1680.             if(dur_timer(&rp->timer) != 0)
  1681.                 rt_drop(rp->target,(unsigned)bits);
  1682.             /* rp will be undefined here if(dur_timer(&rp->timer) */
  1683.         }
  1684.             rt_drop(rp->target,(unsigned)bits);
  1685.  
  1686.     if((rp = rt_blookup(0,0)) != NULLROUTE && dur_timer(&rp->timer) != 0)
  1687.      rt_drop(0,0);    /* delete non-manual default route */
  1688.  
  1689.     /* Temporarily remove all 32-bit specific manual routes. This will make
  1690.      * it possible to prevent loops in findlowroute()
  1691.      */
  1692.     for(i = 0; i < HASHMOD; i++) {
  1693.     saved[i] = Routes[31][i];
  1694.     Routes[31][i] = NULLROUTE;
  1695.     }
  1696.  
  1697.     for(;;) {
  1698.     lowadj = NULLADJ;
  1699.     lowp = NULLCHAR;
  1700.     low = 255;
  1701.     for(adj = Adjs; adj != NULLADJ; adj = adj->next){
  1702.        if(adj->scratch == NULL)
  1703.         continue;        /* skip unreachable adjacency */
  1704.         if(!adj->added){
  1705.         if(adj->cost <= low){
  1706.             low = adj->cost;
  1707.             lowadj = adj;
  1708.             lowp = NULLCHAR;
  1709.         }
  1710.         }
  1711.         else
  1712.         if((r = findlowroute(adj->addr,&low,adj->cost,&lowaddr,&bits))
  1713.           != NULLCHAR) {
  1714.             lowp = r;
  1715.             gateway = adj;
  1716.             lowadj = NULLADJ;
  1717.             }
  1718.     }
  1719.     if(lowadj != NULLADJ){
  1720.         lowadj->added++;
  1721.         rp = rt_add(lowadj->addr,32,0,
  1722.            ((struct rspfiface *)lowadj->scratch)->iface,
  1723.            (int32)lowadj->cost,0,0);
  1724.         /* set the timer to a dummy value. This makes it possible to
  1725.          * distinguish between manual and RSPF-generated routes.
  1726.          * The timer is never started however since RSPF handles the
  1727.          * expiration of routes itself.
  1728.          */
  1729.         set_timer(&rp->timer,MSPTICK);
  1730.         continue;
  1731.     }
  1732.     if(lowp != NULLCHAR){
  1733.         /* Check if we already have this one */
  1734.         rp = rt_blookup(lowaddr,(unsigned)bits);
  1735.         if((rp == NULLROUTE || (rp != NULLROUTE && 
  1736.             rp->metric > (int32) low)) && !ismyaddr(lowaddr)) {
  1737.          /* This is a new route, or a route with strict lower cost than
  1738.           * the previous route (possible only if it was manual)
  1739.           */
  1740.         rp = rt_add(lowaddr,(unsigned)bits,gateway->addr,
  1741.                ((struct rspfiface *)gateway->scratch)->iface,
  1742.                (int32)low,0,0);
  1743.         set_timer(&rp->timer,MSPTICK); /* a dummy value */
  1744.         }
  1745.         *lowp |= 128; /* mark the route as added in any case */
  1746.     }
  1747.     else
  1748.         break; /* no more routes */
  1749.     }
  1750.  
  1751.     /* Add the saved 32 bit routes, if there isn't now a better route */
  1752.     for(i = 0; i < HASHMOD; i++) {
  1753.     rp = saved[i];
  1754.     while(rp != NULLROUTE) {
  1755.          rp2 = rt_blookup(rp->target,32);
  1756.          if(rp2 == NULLROUTE || (rp2 != NULLROUTE && rp2->metric >= rp->metric)) {
  1757.           rp2 = rt_add(rp->target,32,rp->gateway,rp->iface,rp->metric,
  1758.                    0,rp->flags & RTPRIVATE);
  1759.           rp2->uses = rp->uses;
  1760.          }
  1761.          rp2 = rp->next;
  1762.          stop_timer(&rp->timer); /* Stop timer before free() -- KZ1F */
  1763. #ifndef KZ1F
  1764.          free((char *)rp);
  1765. #endif
  1766.          rp = rp2;
  1767.     }
  1768.     }
  1769.  
  1770.     /* Reset all flags */
  1771.     for(adj = Adjs; adj != NULLADJ; adj = adj->next) {
  1772.     adj->added = 0;
  1773.     adj->scratch = NULL;
  1774.     }
  1775.  
  1776.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next){
  1777.     i = len_p(rr->data) - RSPFNODELEN;
  1778.     /* jump past the node header */
  1779.     if(dup_p(&bp,rr->data,RSPFNODELEN,i) != i) {
  1780.          free_p(bp);
  1781.          continue;
  1782.     }
  1783.     adjcnt = 0;
  1784.     while(i > 0){
  1785.         if(adjcnt){
  1786.         if(!Keeprouter && (*bp->data & 128) == 0) {
  1787.              /* This router has an adjacency that was not added. That
  1788.               * means that the router is unreachable. Mark the
  1789.               * stored routing update for deletion.
  1790.               */
  1791.              free_p(bp);
  1792.              free_p(rr->data);
  1793.              rr->data = NULLBUF;    /* indicates disposal */
  1794.              break;
  1795.         }
  1796.         *bp->data &= ~128;    /* Clear the "added" bit */
  1797.         pullup(&bp,NULLCHAR,5);
  1798.         i -= 5;
  1799.         --adjcnt;
  1800.         continue;
  1801.         }
  1802.         ntohrspflink(&linkh,&bp);
  1803.         adjcnt = linkh.adjn;
  1804.         i -= RSPFLINKLEN;
  1805.         }
  1806.     }
  1807.  
  1808.     if(Keeprouter)    /* nothing more to do */
  1809.      return;
  1810.     /* Delete any routers that were discovered as being unreachable */
  1811.     rrprev = NULLRROUTER;
  1812.     rr = Rspfrouters;
  1813.     for(;;) {
  1814.      for(rrprev = NULLRROUTER, rr = Rspfrouters; rr != NULLRROUTER;
  1815.          rrprev = rr, rr = rr->next)
  1816.           if(rr->data == NULLBUF) {
  1817.            if(rrprev != NULLRROUTER)
  1818.             rrprev->next = rr->next;
  1819.            else
  1820.             Rspfrouters = rr->next;
  1821.            free((char *)rr);
  1822.            break;
  1823.           }
  1824.      if(rr == NULLRROUTER)
  1825.           break;
  1826.     }
  1827. }
  1828.  
  1829. /* Find a route from addr with the lowest cost. Returns a pointer to the
  1830.  * buffer that keeps the significant bit count of the selected route.
  1831.  */
  1832. static char *
  1833. findlowroute(addr,low,add,resaddr,resbits)
  1834. int32 addr;            /* The node whose routes will be examined */
  1835. int *low;            /* Lowest cost so far */
  1836. int add;            /* Cost of this node */
  1837. int32 *resaddr;            /* Resulting route stored here */
  1838. int *resbits;            /* Significant bits of resulting route */
  1839. {
  1840.     struct mbuf *bp;
  1841.     struct rspfrouter *rr;
  1842.     struct rspflinkh linkh;
  1843.     struct route *rp;
  1844.     char *r, *retval = NULLCHAR;
  1845.     int adjcnt = 0;
  1846.  
  1847.     linkh.cost = 0;
  1848.     if((rr = rr_lookup(addr)) == NULLRROUTER)
  1849.     return NULLCHAR;
  1850.     if((rp = rt_blookup(addr,32)) != NULLROUTE && rp->metric < add)
  1851.      return NULLCHAR;    /* already added this one, no loops thanks */
  1852.     if(dup_p(&bp,rr->data,RSPFNODELEN,len_p(rr->data) - RSPFNODELEN) !=
  1853.        len_p(rr->data) - RSPFNODELEN) {
  1854.      free_p(bp);
  1855.      return NULLCHAR;
  1856.     }
  1857.     while(len_p(bp) > 0){
  1858.     if(adjcnt){
  1859.         if(*bp->data & 128) {
  1860.         (void) PULLCHAR(&bp);
  1861.         if((r = findlowroute(pull32(&bp),low,add+linkh.cost,resaddr,
  1862.           resbits)) != NULLCHAR)
  1863.              retval = r;
  1864.         }
  1865.         else {
  1866.         *low = add + linkh.cost;
  1867.         retval = bp->data;
  1868.         *resbits = PULLCHAR(&bp);
  1869.         *resaddr = pull32(&bp);
  1870.         pullup(&bp,NULLCHAR,5*(adjcnt-1));
  1871.         adjcnt = 1;    /* No need to check the rest of this link */
  1872.         }
  1873.         --adjcnt;
  1874.         continue;
  1875.     }
  1876.     ntohrspflink(&linkh,&bp);
  1877.     if((add + linkh.cost) <= *low)
  1878.         adjcnt = linkh.adjn;
  1879.     else
  1880.         pullup(&bp,NULLCHAR,5*linkh.adjn);
  1881.     }
  1882.     return retval;
  1883. }
  1884.  
  1885. #ifndef    AX25
  1886. /*
  1887.  * Dummy stub for RSPF if AX25 is not configured.
  1888.  */
  1889. static struct lq *
  1890. al_lookup(ifp,addr,sort)
  1891. struct iface *ifp;
  1892. char *addr;
  1893. int sort;
  1894. {
  1895.     return NULLLQ;
  1896. }
  1897. #endif    /* AX25 */
  1898.